package com.eco.fanliapp.controller.app;


import com.eco.fanliapp.common.base.BaseController;
import com.eco.fanliapp.common.response.ServerResponse;
import com.eco.fanliapp.entity.AppUser;
import com.eco.fanliapp.service.IGoodsFreeService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/mobile/goodsFree")
public class GoodsFreeBuyController extends BaseController {

    @Autowired
    private IGoodsFreeService goodsFreeService;


    /**
     * 查询免单商品（商品未失效）
     * @param currentPage
     * @param pageSize
     * @return
     */
    @GetMapping("/findAllGoodsFree")
    @ResponseBody
    public ServerResponse<PageInfo> findAllGoodsFree(@RequestParam(defaultValue = "1") Integer currentPage, @RequestParam(defaultValue = "5") Integer pageSize) {

        return goodsFreeService.selectAllGoodsFree(currentPage, pageSize);
    }

    /**
     * 查询用户是否可以购买免单商品
     *
     * @param userId
     * @return
     */
    @GetMapping("/checkHaveBuy")
    @ResponseBody
    public ServerResponse<String> checkHaveBuy(Long userId, Integer isTaskUse) {

        AppUser u = getAppUser();
        if (isTaskUse == null || (int) isTaskUse != 1) {
            if (null == u || u.getUserId() == null) {
                return ServerResponse.createBySuccessCodeMessage(301, "用户不存在");
            }

            userId = u.getUserId();
        }

        return goodsFreeService.checkHaveBuy(userId, isTaskUse);
    }

    /**
     * 从免单返回弹窗
     *
     * @return
     */
    @GetMapping("/backRemindPopup")
    @ResponseBody
    public ServerResponse backRemind() {

        AppUser u = getAppUser();

        if (null == u || u.getUserId() == null) {
            return ServerResponse.createBySuccessCodeMessage(301, "用户不存在");
        }

        return goodsFreeService.backRemind(u.getUserId());
    }


}
